package state_;

/**
 * @author DCSGO
 * @version 1.0
 * 线程 “7” 大状态 RUNNABLE -> Ready Running
 * Java 提供的 6 大线程状态
 * NEW 一个尚未启动的线程处于这种状态
 * RUNNABLE 在 Java 虚拟机中执行的线程处于这种状态
 * BLOCKED 被阻塞等待监视器锁定的线程处于这种状态
 * WAITING 无限期等待另一个线程执行特定操作的线程处于这种状态
 * TIMED_WAITING 等待另一个线程执行操作长达指定等待时间的线程处于此状态
 * TERMINATED 已退出的线程处于这种状态
 */
public class State {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        //                                          获取线程状态
        System.out.println(t.getName() + " 状态 " + t.getState());
        t.start();
        //线程的各种状态可以通过 Thread.State 来获取
        while (t.getState() != Thread.State.TERMINATED) {
            System.out.println(t.getName() + " 状态 " + t.getState());
            Thread.sleep(1000);
        }
        System.out.println(t.getName() + " 状态 " + t.getState());
        /*
         * Thread-0 状态 NEW
         * Thread-0 状态 RUNNABLE
         * Thread-0 0
         * Thread-0 状态 TIMED_WAITING
         * Thread-0 1
         * Thread-0 2
         * Thread-0 状态 RUNNABLE
         * Thread-0 3
         * Thread-0 状态 TIMED_WAITING
         * Thread-0 4
         * Thread-0 状态 TIMED_WAITING
         * Thread-0 状态 TIMED_WAITING
         * Thread-0 状态 TERMINATED
         *
         * 进程已结束，退出代码为 0
         */
    }
}

class T extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(currentThread().getName() + " " + i);
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
